let storage = bui.storage();
loader.define(function(require, exports, module, global) {
    var pageview = {
        init(){
            let uiLoading = bui.loading({
                appendTo:"#articleLookLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"加载中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });
            uiLoading.show()

            let showFlag = false
            router.$("#articleMenu").hide()

            // 自定义居中弹出框
            let lookDialog = bui.dialog({
                id: "#lookDialog",
                fullscreen: true,
                mask: true
            });
            router.$('#lookDialog').click(function () {
                lookDialog.close()
            })

            const url = window.location.href
            let artile;
            let glUserid = ''
            let param = url.split("?")[1]
            let params = param.split("&")
            let id = params[0].split("=")[1]
            let gl= params[1].split("=")[1]
            if (gl && gl != 'null') {
                glUserid = gl
            }
            artile = {id:id, glUserid: glUserid}

            // let ks = new Date().getTime()
            //1、创建ajax引擎对象 ---- 所有的操作都是通过引擎对象
            let xmlHttp = new XMLHttpRequest();
            //2、绑定监听 ---- 监听服务器是否已经返回相应数据
            xmlHttp.onreadystatechange = function(){
                let res = xmlHttp.response
                if (res) {
                    res = eval('(' + res + ')')
                    let data = res.data

                    router.$("#cateTab").html(data.cateStr+"信息")
                    router.$("#time").html(formatDate(data.ptime) + " 发布")
                    router.$("#country2").html(data.countryStr + " "+ data.cityStr)
                    // if (data.userStr) {
                    //     router.$("#concat").html(data.userStr)
                    //     router.$("#lxr").show()
                    // }
                    if (data.tel) {
                        router.$("#phone").html(data.tel)
                        router.$("#dh").show()
                        router.$("#phoneBtn").attr("href","tel:" + data.tel)
                        router.$("#phoneBtn").show()

                        router.$("#articleTel").attr("href","tel:" + data.tel)
                    }
                    router.$("#articleTel").click(function () {
                        if (!data.tel) {
                            bui.alert("发布者未填写手机号信息！")
                        }
                    })
                    router.$("#copy").click(function () {
                        if(data.wx) {
                            new ClipboardJS('.btn',{text:function(trigger) { return data.wx;} });
                            bui.alert("复制成功！")
                        } else {
                            bui.alert("发布者未填写微信号信息！")
                        }
                    })
                    if (data.wx) {
                        router.$("#wx").html(data.wx)
                        router.$("#weixin").show()
                    }
                    if (data.email) {
                        router.$("#email").html(data.email)
                        router.$("#youxiang").show()
                    }

                    if (!data.title1)  {
                        router.$("#title1").html(data.title)
                        router.$("#title1").show()
                    } else {
                        router.$("#title1").html(data.title1)
                        router.$("#title1").show()
                    }
                    router.$("#content1").html(data.content)

                    router.$("#content1 img").click(function () {
                        let url = $(this).attr("src")
                        router.$("lookImg").attr("src", url)
                        lookDialog.open()
                    })

                    uiLoading.stop()
                }
            }
            //3、绑定地址
            xmlHttp.open("POST",global.api.getAdvertiseInfo,false);
            //4、发送请求
            xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlHttp.send("id=" + artile.id);

            bui.ajax({
                url: global.api.getAdvertiseInfoOther,
                data: {id: artile.id},
                method: "POST"
            }).then(function(res){
                let data = res.data

                if (data) {
                    if (data.qrcode) {
                        router.$("#glyQrcodeK").show()
                        router.$("#qrcode2").attr('src', data.qrcode)
                    }

                    console.log('footer')
                    console.log(data.userFooter)
                    if (data.userFooter) {
                        router.$("#articleFooter").html(data.userFooter)
                        router.$("#articleFooter").show()
                    }

                    let org = data.org
                    if (org.qrcode) {
                        router.$("#qrcode1").attr('src', org.qrcode)
                    } else {
                        router.$("#qrcode1").attr('src', 'images/qrcode.jpg')
                    }

                    //加载广告
                    bui.ajax({
                        url: global.api.getAdvertiseDetailList,
                        data: {city: org.id, redisCity: org.name,country: org.parent, userId: glUserid},
                        method: "POST"
                    }).then(function(res){
                        let ggArr = res.data

                        let arr = []

                        if (ggArr && ggArr.length > 0) {
                            let len = ggArr.length

                            let index = 0
                            if (len > 1) {
                                index = Math.floor(Math.random()*ggArr.length)
                                arr.push(index)
                            }
                            router.$("#advertise1").attr("src", ggArr[index].imgUrl)
                            router.$("#advertise1").attr("name", ggArr[index].article + '~' + ggArr[index].title)
                            router.$("#advertise1").show()

                            if (len > 1) {
                                index = Math.floor(Math.random()*ggArr.length)
                                while (arr.indexOf(index) > -1) {
                                    index = Math.floor(Math.random()*ggArr.length)
                                    if (arr.indexOf(index) === -1) {
                                        arr.push(index)
                                        break;
                                    }
                                }
                                router.$("#advertise12").attr("src", ggArr[index].imgUrl)
                                router.$("#advertise12").attr("name", ggArr[index].article + '~' + ggArr[index].title)
                                router.$("#advertise12").show()
                            }

                            if (len > 2) {
                                router.$("#ggk").show()

                                if (len > 2) {
                                    index = Math.floor(Math.random()*ggArr.length)
                                    while (arr.indexOf(index) > -1) {
                                        index = Math.floor(Math.random()*ggArr.length)
                                        if (arr.indexOf(index) === -1) {
                                            arr.push(index)
                                            break;
                                        }
                                    }
                                    router.$("#advertise2").attr("src", ggArr[index].imgUrl)
                                    router.$("#advertise2").attr("name", ggArr[index].article + '~' + ggArr[index].title)
                                    router.$("#advertise2").show()
                                }

                                if (len > 3) {
                                    index = Math.floor(Math.random()*ggArr.length)
                                    while (arr.indexOf(index) > -1) {
                                        index = Math.floor(Math.random()*ggArr.length)
                                        if (arr.indexOf(index) === -1) {
                                            arr.push(index)
                                            break;
                                        }
                                    }
                                    router.$("#advertise3").attr("src", ggArr[index].imgUrl)
                                    router.$("#advertise3").attr("name", ggArr[index].article + '~' + ggArr[index].title)
                                    router.$("#advertise3").show()
                                }

                                if (len > 4) {
                                    index = Math.floor(Math.random()*ggArr.length)
                                    while (arr.indexOf(index) > -1) {
                                        index = Math.floor(Math.random()*ggArr.length)
                                        if (arr.indexOf(index) === -1) {
                                            arr.push(index)
                                            break;
                                        }
                                    }
                                    router.$("#advertise4").attr("src", ggArr[index].imgUrl)
                                    router.$("#advertise4").attr("name", ggArr[index].article + '~' + ggArr[index].title)
                                    router.$("#advertise4").show()
                                }
                            }
                        }
                    })
                } else {
                    bui.confirm({
                        "title": "",
                        "height": 460,
                        "content": '<div class="bui-box-center"><div><h3>当前文章已消失！</h3></div></div>',
                        "buttons": [{ name: "确定", className: "primary-reverse" }],
                        callback:function(){
                            this.close();
                            bui.back()
                        }
                    });
                }
            })

            // 自定义居中弹出框
            let gzDialog = bui.dialog({
                id: "#gzDialog1",
                height: 300,
                // mask: false,
                callback: function(e) {}
            });
            router.$("#gz").click(function () {
                gzDialog.open()
            })

            //底部操作按钮
            router.$("#ly").click(function () {
                document.getElementById('lyK').scrollIntoView()
            })
            router.$("#gzMy").click(function () {
                gzDialog.open()
            })
            router.$("#lxfs").click(function () {
                if(showFlag) {
                    showFlag = false
                    router.$("#articleMenu").hide()
                } else {
                    showFlag = true
                    router.$("#articleMenu").show()
                }
            })
        }
    };


    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})

function formatDate(time) {
    let date = new Date(time * 1000);
    let now = new Date();
    let days=parseInt((now - date) / (1000 * 60 * 60* 24) + '');
    if (days > 7){
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        let D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
        let h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
        let m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
        let s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
        return Y+M+D+h+m+s
    } else if (days < 1) {
        let hour=parseInt((now - date) / (1000 * 60 * 60) + '');
        return hour + "小时前"
    }
    return days + "天前"
}
